--金币礼包/通用特殊购买。。。
local Util = require("common.Util")
local Log = require("common.Log")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local PresentExcel = require("excel.present")
local BuyExcel = require("excel.buy")
local BuyLogic = require("topup.BuyLogic")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local SceneHandler = require("scene.Handler")

local function fontSpecificNet(net, human, id)
	local config = PresentExcel.specificShop[id]
	net.id = id
	net.order = config.order
	Grid.makeItem(net.item, config.itemId, config.cnt)
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
	net.vipexp = config.vipExp	
end

function query(human, id)
	local specificConf = PresentExcel.specific[id]
	if specificConf == nil then
	    return
	end

	local msgRet = Msg.gc.GC_SPECIFIC_QUERY
	local net = msgRet.data
	net.id = id
	net.needZuanshi = specificConf.needZhuanShi	
	local itemID = specificConf.reward[1] or 0
	local itemCnt = specificConf.reward[2] or 0
	net.reward[0] = 0
	if itemID > 0 then
	   net.reward[0] = 1
	   Grid.makeItem(net.reward[1], itemID, itemCnt)
	end
	net.list[0] = 0
	for _, spid in pairs(specificConf.shopId) do
		net.list[0] = net.list[0] + 1
		fontSpecificNet(net.list[net.list[0]], human, spid)
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

function buy(human, id, buyCnt)
    if buyCnt > 30000 or buyCnt <= 0 then
	   return
	end

    local specificConf = PresentExcel.specific[id]
	if specificConf == nil then
	    return
	end
	if specificConf.reward[1] == nil then
		return -- 不卖东西哦
	end
	
	local needZs = specificConf.needZhuanShi
    -- 钻石判断
	local needZuanshi = buyCnt * needZs
	if human.db.zuanshi < needZuanshi then 
		return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
	end
	-- 扣钻石
	ObjHuman.decZuanshi(human, -needZuanshi, "buySpecific")
	
	local itemID = specificConf.reward[1]
	local itemCnt = specificConf.reward[2] * buyCnt	
	BagLogic.cleanMomentItemList()
    BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, itemID, itemCnt)
    BagLogic.addMomentItemList(human, "buySpecific")
	query(human, id)	 
end

function isOpen(human)
	if not SceneHandler.canCharge(human) then
		return
	end
	
	return true
end